package com.hngy.xpq.android_sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MyHelper myhelper;
    EditText names , phones;
    Button add , query , delete , update;
    ListView list,list1;
    List<String> ids;
    List<String> data;
    List<String> next;
    String update_id = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myhelper = new MyHelper(this);

        names = findViewById(R.id.phone_name);
        phones = findViewById(R.id.phone_phone);

        add = findViewById(R.id.phone_add);
        query = findViewById(R.id.phone_query);
        delete = findViewById(R.id.phone_delete);
        update = findViewById(R.id.phone_update);

        list = findViewById(R.id.phone_list);
        list1 = findViewById(R.id.phone_list1);

        add.setOnClickListener(this);
        query.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                names.setText(data.get(position));
                phones.setText(next.get(position));
                update_id = ids.get(position);
            }
        });

        list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Uri uri = Uri.parse( "tel:"+ next.get(position));
                Intent intent = new Intent(Intent.ACTION_DIAL,uri);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        String name , phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()){
            case R.id.phone_add:
                name = names.getText().toString();
                phone = phones.getText().toString();
                if (TextUtils.isEmpty(names.getText())){
                    Toast.makeText(this,"姓名不能为空!!",Toast.LENGTH_SHORT).show();
                    break;
                } else if (TextUtils.isEmpty(phones.getText())){
                    Toast.makeText(this,"电话号码不能为空!!",Toast.LENGTH_SHORT).show();
                    break;
                }
                db = myhelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name",name);
                values.put("phone",phone);
                db.insert("listphone",null,values);
                Toast.makeText(this,"信息添加成功",Toast.LENGTH_SHORT).show();
                db.close();
                names.setText(null);
                phones.setText(null);
                break;
            case R.id.phone_query:
                db = myhelper.getReadableDatabase();
                Cursor cursor = db.query("listphone",null,null,null,null,null,null);
                cursor.moveToFirst();
                data = new ArrayList<>();
                next = new ArrayList<>();
                ids = new ArrayList<>();
                if (cursor.getCount() == 0){
                    Toast.makeText(this,"通讯录为空!!",Toast.LENGTH_SHORT).show();
                } else {
                    ids.add(cursor.getString(0));
                    data.add(cursor.getString(1));
                    next.add(cursor.getString(2));
                } while (cursor.moveToNext()){
                    ids.add(cursor.getString(0));
                    data.add(cursor.getString(1));
                    next.add(cursor.getString(2));
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.phine_list,R.id.li,data);
                ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this,R.layout.phine_list,R.id.li,next);
                list.setAdapter(adapter);
                list1.setAdapter(adapter1);
                cursor.close();
                db.close();
                break;
            case R.id.phone_update:
                db = myhelper.getReadableDatabase();
                values = new ContentValues();
                if (TextUtils.isEmpty(names.getText())){
                    Toast.makeText(this,"所需要修改的姓名不能为空!!",Toast.LENGTH_SHORT).show();
                    break;
                } else if (update_id == null){
                    values.put("phone" , phone = phones.getText().toString());
                    db.update("listphone",values,"name=?",new String[]{names.getText().toString()});
                    Toast.makeText(this,"信息修改成功!!",Toast.LENGTH_SHORT).show();
                } else {
                    values.put("name" , phone = names.getText().toString());
                    values.put("phone" , phone = phones.getText().toString());
                    db.update("listphone",values,"_id=?",new String[]{update_id});
                    Toast.makeText(this,"信息修改成功!!",Toast.LENGTH_SHORT).show();
                }
                db.close();
                update_id = null;
                names.setText(null);
                phones.setText(null);
                break;
            case R.id.phone_delete:
                db = myhelper.getReadableDatabase();
                if (TextUtils.isEmpty(names.getText())){
                    Toast.makeText(this,"请输入需要删除的联系人!!",Toast.LENGTH_SHORT).show();
                    break;
                }
                db.delete("listphone", "name=?",new String[]{names.getText().toString()});
                Toast.makeText(this,"信息删除成功!!",Toast.LENGTH_SHORT).show();
                db.close();
                names.setText(null);
                phones.setText(null);
                list.setAdapter(null);
                list1.setAdapter(null);
                break;
        }
    }

    class MyHelper extends SQLiteOpenHelper {
        public MyHelper(Context context) {
            super(context, "ListPhone.db", null , 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE listphone(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20),  phone VARCHAR(20))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}